//
//  SEGcustomContentViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGcustomContentViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>
#import "SEGPlatformManager.h"
#import "UIColor+YYAdd.h"
@implementation SEGcustomContentViewModel
-(instancetype)init{
    if (self == [super init]) {
        _contentText = @"";
        _like = @"";
        _isLike = @"";
        _comment = @"";
        _widgetWidth = LYScreenWidth();
    }
    return self;
}
@end


/**************************SEGcustomContentViewLayout************************/
@implementation SEGcustomContentViewLayout
- (instancetype)initWithData:(SEGcustomContentViewModel *)data type:(SEGEnumcustomContentViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segcustomContentViewLayout];
    return self;
}

-(void)segcustomContentViewLayout{
    //高度固定
    //    _totalHeight = kcustomContentViewHeight;
    
    //高度不固定
    _totalHeight = 0;
    _contentTextLabelHeight = 0;
    _contentTextLabelTextLayout = nil;
    _likeLabelHeight = 0;
    _likeLabelTextLayout = nil;
    _commentLabelHeight = 0;
    _commentLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    [self segcontentTextLabelWithContent:_data.contentText helpLabelText:_data.helpLabelText fontSize:15 textColor:kColor_1];
    [self seglikeLabelWithContent:_data.like fontSize:13 textColor:kColor_3];
    [self segcommentLabelWithContent:_data.comment fontSize:13 textColor:kColor_3];
    
    _commentTopPadding = 5;//默认为5
    switch (_type) {
        case SEGEnumcustomContentViewType_NEIGHBOUR_PHOTOText:
        {
            [self segNeighbourLayout];
            if (_data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
                _totalHeight =  _contentTextLabelHeight + _neighbourLayout.totalHeight + 15;
            }else {
                _totalHeight = _contentTextLabelHeight + _neighbourLayout.totalHeight + _commentTopPadding + _commentLabelHeight + 5;
            }
            break;
        }
        case SEGEnumcustomContentViewType_TOPIC_TEXTONLY:
        {
            [self segTopicTextLayout];
            _totalHeight = _topicTextLayout.totalHeight + _commentTopPadding + _commentLabelHeight + 5;
            break;
        }
        case SEGEnumcustomContentViewType_TOPIC_PHOTOText:
        {
            [self segTopicPicLayout];
            _totalHeight = _topicPicLayout.totalHeight + _contentTextLabelHeight + viewPadding + _commentTopPadding + _commentLabelHeight + 5;
            break;
        }
        case SEGEnumcustomContentViewType_MARKET:
        {
            [self segMarketLayout];
            if (_data.isNeighborListView) {
                _totalHeight += _marketLayout.totalHeight + 42; // 更多按钮高度12 + 上下间距30
            }else {
                _totalHeight += _marketLayout.totalHeight + 15;
            }
            break;
        }
        case SEGEnumcustomContentViewType_FOCUS:
        {
            [self segFocusLayout];
            if (_data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
                _totalHeight =  _contentTextLabelHeight + 10 + _focusLayout.totalHeight + 15;
            }else {
                _totalHeight = _contentTextLabelHeight + 10 + _focusLayout.totalHeight + _commentTopPadding + _commentLabelHeight + 5;
            }
            break;
        }
        case SEGEnumcustomContentViewType_LIKE:
        {
            [self segFocusLayout];
            _totalHeight = _focusLayout.totalHeight + 15;
            break;
        }
        case SEGEnumcustomContentViewType_MARKETDETAIL:
        {
            [self segMarketDetailLayout];
            if (_data.isDetailView) {//邻里圈详情界面不显示评论点赞按钮
                _totalHeight =  _marketDetailLayout.totalHeight + 15;
            }else {
                _totalHeight = _marketDetailLayout.totalHeight + _commentTopPadding + _commentLabelHeight + 5;
            }
            break;
        }
    }
}

-(void)segNeighbourLayout{
    _data.NeighbourDataSource.widgetWidth = _data.widgetWidth;
    _neighbourLayout = [[SEGNeighbourViewLayout alloc]initWithData:_data.NeighbourDataSource];
}

-(void)segMarketLayout{
    _data.marketDataSource.widgetWidth = _data.widgetWidth;
    _marketLayout = [[SEGMarketViewLayout alloc]initWithData:_data.marketDataSource];
}

-(void)segFocusLayout{
    _data.focusDataSource.widgetWidth = _data.widgetWidth;
    _focusLayout = [[SEGFocusViewLayout alloc] initWithData:_data.focusDataSource];
}

-(void)segTopicTextLayout{
    //    //话题标题不能与点赞评论重合
    //    _data.topicTextDataSource.topicTitleWidth = _data.widgetWidth - _commentLabelWidth - _likeLabelWidth - likeCommentPadding;
    _data.topicTextDataSource.widgetWidth = _data.widgetWidth;
    _topicTextLayout = [[SEGTopicTextViewLayout alloc]initWithData:_data.topicTextDataSource];
}

-(void)segTopicPicLayout{
    //    //话题标题不能与点赞评论重合
    //    _data.topicPicDataSource.topicTitleWidth = _data.widgetWidth - _commentLabelWidth - _likeLabelWidth - likeCommentPadding;
    _data.topicPicDataSource.widgetWidth = _data.widgetWidth;
    _topicPicLayout = [[SEGTopicPicViewLayout alloc]initWithData:_data.topicPicDataSource];
    
    // 控件数据模型创建
    SEGAutoHeightImageViewModel *imageViewData = [[SEGAutoHeightImageViewModel alloc]init];
    if (_data.topicPicDataSource.picUrlStrArray && _data.topicPicDataSource.picUrlStrArray.count > 0) {
        imageViewData.pic = _data.topicPicDataSource.picUrlStrArray[0];
        imageViewData.picCount = _data.topicPicDataSource.picUrlStrArray.count;
    }
    imageViewData.pictureWidth = _data.widgetWidth;
    // 控件内容布局计算
    SEGAutoHeightImageViewLayout *imageViewlayout = [[SEGAutoHeightImageViewLayout alloc]initWithData:imageViewData type:SEGEnumAutoHeightImageViewType_NO];
    // 内容布局填充到视图
    //    SEGAutoHeightImageView *varAutoHeightImageView = [[SEGAutoHeightImageView alloc]initWithFrame:CGRectZero];
    _topicPicLayout.autoHeightImageViewLayout = imageViewlayout;
}

-(void)segMarketDetailLayout{
    _marketDetailLayout = [[SEGMarketDetailViewLayout alloc]initWithData:_data.marketDetailDataSource type:SEGEnumMarketDetailViewType_NO];
}

//控件contentTextLabel文本布局计算
-(void)segcontentTextLabelWithContent:(NSString *)content
                        helpLabelText:(NSString *)helpLabelText
                             fontSize:(CGFloat)fontSize
                            textColor:(UIColor*)color
{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    
    if (helpLabelText && helpLabelText.length > 0) {//有互助标签
        NSString *typeText = [NSString stringWithFormat:@" %@ ",helpLabelText];
        NSString *contentText = [NSString stringWithFormat:@"%@  %@", typeText, content];//加上图片位置计算行数
        
        text = [[NSMutableAttributedString alloc] initWithString:contentText];
        text.yy_font = font;
        text.yy_color = color;
        
        NSRange typeRange = [contentText rangeOfString:typeText];
        SEGTextBorder *border = [SEGTextBorder borderWithFillColor:kColor_T cornerRadius:2];
        border.insets = UIEdgeInsetsMake(-1, 0, -1, 0);
        [text yy_setTextBackgroundBorder:border range:typeRange];
        [text yy_setColor:[UIColor colorWithHexString:@"#333333"] range:typeRange];
        [text yy_setFont:kFont_13 range:typeRange];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 3;
    modifier.paddingBottom = 3;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    
    _contentTextLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentTextLabelTextLayout)return;
    _contentTextLabelHeight = [modifier heightForLineCount:_contentTextLabelTextLayout.rowCount];
}

//控件likeLabel文本布局计算
-(void)seglikeLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    if (content.integerValue <= 0) {//数字小于等于0不展示
        content = @"";
    }
    
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    //按钮图片
    CGFloat padding = 6;
    if(content.length == 0) {//没有文字时不需要间距
        padding = 0;
    }
    UIImage *image;
    if(_data.isLiked) {//已点赞
        image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_zan_pre")];//btn_huati_zan_gray_pre
    }else {
        image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_zan_nor")];//btn_huati_zan_gray_nor
    }
    NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:image FontSize:image.size.height contentInsets:UIEdgeInsetsMake(0, 10, 0, padding)];//直接传字体大小会导致图片变形，传图片高度
    if (img) {
        [text insertAttributedString:img atIndex:0];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    container.linePositionModifier = modifier;
    
    _likeLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_likeLabelTextLayout)return;
    _likeLabelHeight = [modifier heightForLineCount:_likeLabelTextLayout.rowCount] + 20;//上下各留10，加大点击区域
    CGFloat w = [SEGKitHelper seg_sizeWithString:content fontSize:fontSize textColor:color].width;
    _likeLabelWidth = w + padding + image.size.width + 20;
}

//控件 commentLabel文本布局计算
-(void)segcommentLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    if (content.integerValue == 0) {//数字为0不展示
        content = @"";
    }
    
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //按钮图片
    CGFloat padding = 6;
    if(content.length == 0) {//没有文字时不需要间距
        padding = 0;
    }
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_comment")];
    NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:image FontSize:image.size.height contentInsets:UIEdgeInsetsMake(0, 10, 0, padding)];
    if (img) {
        [text insertAttributedString:img atIndex:0];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    container.linePositionModifier = modifier;
    
    _commentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_commentLabelTextLayout)return;
    _commentLabelHeight = [modifier heightForLineCount:_commentLabelTextLayout.rowCount] + 20;//上下各留10，加大点击区域
    CGFloat w = [SEGKitHelper seg_sizeWithString:content fontSize:fontSize textColor:color].width;
    _commentLabelWidth = w + padding + image.size.width + 20;

}
@end

